//
//  PhotoTableView.swift
//  DMCamera
//
//  Created by 李浩 on 2017/8/3.
//  Copyright © 2017年 李浩. All rights reserved.
//

import UIKit
import Photos

class PhotoTableView: UITableView, UITableViewDelegate, UITableViewDataSource {

    var dataArr: [PHAssetCollection] = []
    
    var itemClick = {(photo: PHAssetCollection) -> Void in}
    
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        delegate = self
        dataSource = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        let coll = dataArr[indexPath.row]
        if (coll.localizedTitle != nil) {
            cell?.textLabel?.text = String(format: "%@ ======= %d", coll.localizedTitle!, coll.estimatedAssetCount)
        } else {
            cell?.textLabel?.text = String(format: "%@ ======= %d", "空", coll.estimatedAssetCount)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let coll = dataArr[indexPath.row]
        itemClick(coll)
    }
}
